<?php
use yii\bootstrap\ActiveForm;
?>
<link rel="stylesheet" href="../static/dist/css/AdminLTE.min.css" type="text/css">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datatables/jquery.dataTables.css" type="text/css">
<link rel="stylesheet" href="../static/css/admin.css" type="text/css">
<link rel="stylesheet" href="../static/jBox/Skins/Metro/jbox.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/bootstrap-select/css/bootstrap-select.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/datepicker/datepicker3.css" type="text/css">
<link rel="stylesheet" href="../static/plugins/daterangepicker/daterangepicker.css" type="text/css">
<style>
    .dataTables_length {
        margin-top: 10px;
    }
    .dataTable>tbody>tr>td {
        vertical-align: middle;
        font-size: 0.8em;
        padding: 8px 18px;
    }
    .dataTable>thead>tr>th {
        vertical-align: middle;
        font-size: 0.8em;
    }
    .source_type_class {
        min-height: 20px;
        padding-left: 20px;
        margin-bottom: 0;
        font-weight: 400;
        cursor: pointer;
    }
    .form-left{
        border-right: 1px solid #dddddd;
    }
    .examine-content{
        width: 1000px;
        margin-left:-300px;
    }
    .examine-content .input-group .form-control{
        margin-bottom: 20px;
        height: 30px ;
        line-height: 30px;
    }
    .examine-content button{
        margin: 10px;
    }
    #table_id_example th, td {
        white-space: nowrap;
    }
</style>
<div class="tabs-panel">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    商品名称：<input class="depart-input" id="searchName" style="width: 160px" type="text">&nbsp;
                    状态：
                    <select id="stockStatus" class="selectpicker" multiple="multiple" title="请选择" style="width: 100px">
                        <option value="0">创建</option>
                        <option value="1">审核</option>
                        <option value="9">作废</option>
                    </select>&nbsp;
                    仓库：
                    <select id="warehouseId" name="warehouseId" class="selectpicker" multiple="multiple" title="请选择" style="width: 100px">
                        <?php foreach($warehouse as $value){?>
                            <option value="<?= $value['id']?>"><?= $value['warehouseName']?></option>
                        <?php }?>
                    </select>
                    <?php if (!$pageFlg) { ?>
                        备注：<input class="depart-input" id="remark" style="width: 160px" type="text">&nbsp;
                        <br><br>
                        预计到货日期: <input class="depart-input" id="expecteDate" style="width: 200px" type="text">
                        <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#expecteDate').val('')">清空</button>&nbsp;
                                                                                                                                          实际到货日期: <input class="depart-input" id="actualDate" style="width: 200px" type="text">
                        <button type="button" class="btn btn-default" style="width: 50px;" onclick="$('#actualDate').val('')">清空</button>&nbsp;
                    <?php } ?>
                    <button type="button" class="btn btn-primary depart-modal" id="select_button" style="width: 80px;">查询</button>&nbsp;
                    <button type="button" class="btn btn-warning depart-modal" id="reset_button" style="width: 80px;">重置</button>
                </div>
                <div class="box-body">
                    <?php if($pageFlg == '0'){ ?>
                        <a type="button" class="btn btn-success depart-modal" href="/stock-tmp/add-receipt" style="width: 80px;">添加</a>
                        <a type="button" class="btn btn-success depart-modal" href="/stock-tmp/add-receipt?pur=1" style="width: 80px;display: none;">采购入库</a>
                    <?php } else { ?>
                        <a type="button" class="btn btn-success depart-modal" href="/stock-tmp/add-revise" style="width: 80px;">添加</a>
                    <?php } ?>
                    <table id="table_id_example" class="display">
                        <thead>
                        <tr>
                            <th style="width: 10%">操作</th>
                            <th style="width: 15%">单号</th>
                            <th style="width: 15%">仓库</th>
                            <th style="width: 10%">状态</th>
                            <th style="width: 15%">创建人</th>
                            <th style="width: 15%">创建时间</th>
                            <th style="width: 10%">数量</th>
                            <th style="width: 10%">金额</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 固定js -->
<script type="text/javascript" src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../static/jBox/jquery.jBox-2.3.min.js"></script>
<script type="text/javascript" src="../static/jBox/i18n/jquery.jBox-zh-CN.js"></script>
<script type="text/javascript" src="/static/plugins/treeTable/jquery.treetable.js"></script>
<!-- 自定义js -->
<script type="text/javascript" src="../static/plugins/moment/moment.min.js"></script>
<script type="text/javascript" src="../static/plugins/bootstrap-select/js/bootstrap-select.js"></script>
<script type="text/javascript" src="../static/plugins/bootstrap-select/js/defaults-zh_CN.js"></script>
<script type="text/javascript" src="../static/plugins/daterangepicker/daterangepicker.js"></script>
<script type="text/javascript" src="../static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/static/app/global.js?version=<?=Yii::$app->params['version']?>"></script>
<script type="text/javascript">
    var pageFlg = <?= isset($pageFlg) ? $pageFlg : 0; ?>;
    var table = null;

    $(function () {
        //复选下拉
        $("#stockStatus,#warehouseId").selectpicker({ actionsBox:true });
        //日期范围
        $('#expecteDate, #actualDate').daterangepicker({
            language:  'zh-CN',
            timePicker: false,
            //timePickerIncrement: 30,
            format: 'YYYY-MM-DD',
            locale: {
                format: 'YYYY-MM-DD',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            }
        });
        $('#expecteDate, #actualDate').val('');

        table = $('#table_id_example').DataTable({
            dom: '<"top">rt<"bottom"flip><"clear">',
            language: {
                "sUrl":'../static/plugins/datatables/language.json'
            },
            "searching": false,
            "processing": true,
            "serverSide": true,
            "scrollY": "550px",
            "scrollCollapse": true,
            "scrollX": true,
            ajax:{
                url:'/stock-tmp/get-list',
                type:'post',
                data:function(d){
                    d.searchName = $('#searchName').val()
                    d.stockStatus = $('#stockStatus').val()
                    d.warehouseId = $('#warehouseId').val()
                    d.remark = $('#remark').val()
                    d.expecteDate = $('#expecteDate').val()
                    d.actualDate = $('#actualDate').val()
                    d.pageFlg = pageFlg;
                },
                error:getError
            },
            aoColumns: [
                { "data": "", "bSortable": false},
                { "data": "stockNo", "bSortable": true },
                { "data": "warehouseName", "bSortable": false },
                { "data": "stockStatus", "bSortable":false },
                { "data": "creator", "bSortable":false },
                { "data": "createTime", "bSortable":false },
                { "data": "totalQty", "bSortable":false },
                { "data": "totalAmount", "bSortable":false },
            ],
            columnDefs: [
                {
                    "targets": [0],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        var html = '';
                        if(full.stockStatus == '0') {
                            html += '<a href="javascript:editStatus(\''+full.stockNo+'\',1)">审核</a>';
                            html += '&nbsp;<a href="javascript:editStatus(\''+full.stockNo+'\',9)">作废</a>';
                        }
                        return html;
                    }
                },
                {
                    "targets": [1],
                    "render": function(data, type, full) {
                        var html = '';
                        if (pageFlg)
                            html = '<a href="/stock-tmp/add-revise?stockNo=' + full.stockNo + '">' + data + '</a>';
                        else
                            html = '<a href="/stock-tmp/add-receipt?stockNo=' + full.stockNo + '">' + data + '</a>';
                        return html;
                    }
                },
                {
                    "targets": [3],
                    "orderable":false,
                    "searchable":false,
                    "render": function(data, type, full) {
                        if(data == '0') return '创建';
                        if(data == '1') return '审核';
                        if(data == '9') return '作废';
                    }
                }
            ],
            order: [[ 1, "desc" ]]
        });

        $("#select_button").on('click', function () {
            table.ajax.reload();
        });

        //重置
        $("#reset_button").on('click', function () {
            $('#searchName').val('');
            $('#remark').val('');
            document.getElementById("warehouseId").options.selectedIndex = -1; //回到初始状态
            $("#warehouseId").selectpicker('refresh');
            document.getElementById("stockStatus").options.selectedIndex = -1; //回到初始状态
            $("#stockStatus").selectpicker('refresh');
            $('#expecteDate').val('');
            $('#actualDate').val('');
            table.ajax.reload();
        });
    });

    //审核、作废
    function editStatus (stockNo, status) {

        $.jBox.confirm("请确认操作", "提示", function(v){
            if (v == 'ok'){
                $.post('/stock-tmp/edit-stock-status', {stockNo:stockNo, status:status},
                    function (data) {
                        if(data.code == 200){
                            $.jBox.tip('操作成功', 'success');
                            table.draw(false);
                        }else{
                            $.jBox.error(data.val, '错误');
                        }
                    },
                    'json'
                );
            }else{
                $.jBox.close();
            }
        });

    }
</script>